package com.sk.waste.app.activitys

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.Button
import android.widget.EditText
import com.lc.smc.lib.annotations.LCBindView
import com.lc.smc.lib.interfaces.LCInterface
import com.lc.smc.lib.utils.LCBaseUtil
import com.lc.smc.lib.utils.LCLogger
import com.lc.smc.lib.utils.LCSPUtil
import com.sk.waste.app.R
import com.sk.waste.app.bases.BaseActiivity
import com.sk.waste.app.config.AppConfig
import com.sk.waste.app.config.NetWorkConfig
import com.sk.waste.app.utils.OkHttpUtil
import okhttp3.Call
import org.json.JSONObject
import java.io.IOException

/**
 *  @time 2025/4/25  17:11
 *  @desc 重置密码
 */
class ResetPassActivity : BaseActiivity() {

    @LCBindView(R.id.ac_resetpaas_password_old)
    private lateinit var ettPassOld: EditText

    @LCBindView(R.id.ac_resetpaas_password_new)
    private lateinit var ettPassNew: EditText

    @LCBindView(R.id.ac_resetpass_word_again)
    private lateinit var ettPassAgain: EditText

    @LCBindView(R.id.ac_resetpass_submit)
    private lateinit var bttSave: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_reset_password)
        super.onCreate(savedInstanceState)
        setTitleColorLight(false)
        setBack()
        setTitleView("重置密码")
        initView()
    }

    fun initView() {
        bttSave.setOnClickListener {
            if (checkParams()) {
                val params = JSONObject()
                params.put("oldPassword", ettPassOld.text.toString().trim())
                params.put("newPassword",ettPassNew.text.toString().trim())
                submitPass(params.toString())
            }
        }
    }

    fun checkParams(): Boolean {
        val oldPass = ettPassOld.text.toString()
        val newPass = ettPassNew.text.toString()
        val newPass2 = ettPassAgain.text.toString()
        if (TextUtils.isEmpty(oldPass)) {
            showToast("请输入旧密码")
            return false
        }
        if (TextUtils.isEmpty(newPass)) {
            showToast("请输入新密码")
            return false
        }
        val regex = "^(?=.*[A-Za-z])(?=.*\\d).+$"
        if (newPass.length < 6 || !newPass.matches(Regex(regex))) {
            showToast("请输入6位以上的密码，包含字母和数字！")
            return false
        }
        if (TextUtils.isEmpty(newPass2)) {
            showToast("请再次输入新密码")
            return false
        }
        if (newPass != newPass2) {
            showToast("两次输入的新密码不一致，请重新输入！")
            return false
        }
        return true
    }

    /**
     *  @time 2025/5/26  14:46
     *  @author Im_jingwei
     *  @desc 保存
     */
    fun submitPass(params: String) {
        val httpUtil = OkHttpUtil()
        val url = NetWorkConfig.NET_URL_ROOT + NetWorkConfig.NET_URL_RESETPASS
        val token = LCSPUtil[mContext, AppConfig.SP_ACCESS_TOKEN]
        httpUtil.httpRequest(url, token, params, object : LCInterface.LcCallBackListener {
            override fun onFailure(call: Call, e: IOException) {
                showToast("操作失败，" + e.message)
            }

            @Throws(IOException::class)
            override fun onSuccess(call: Call, response: String) {
                try {
                    val jsonObject = JSONObject(response)
                    val success = jsonObject.getBoolean("success")
                    val msg = jsonObject.getString("message")
                    if (success) {
                        showToast("修改成功，请重新登录！")
                        clearLoginInfo()
                        LCLogger.d(TAG, "修改成功！,信息：$jsonObject")
                    } else {
                        showToast("修改失败，$msg")
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                    showToast("操作失败，" + e.message)
                }
            }
        })
    }

    /**
     *  @time 2025/6/7  9:05
     *  @desc 清楚登录信息，重新登录
     */
    private fun clearLoginInfo() {
        LCSPUtil.save(mContext, AppConfig.SP_ACCESS_TOKEN, "")
        LCSPUtil.save(mContext, AppConfig.SP_USER_ACCOUNT, "")
        LCSPUtil.save(mContext, AppConfig.SP_USER_PASSWORD, "")
        val intent = Intent(mContext, LoginActivity::class.java)
        startActivity(intent)
    }

}